package com.example.managemyself.ui.diary.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.managemyself.R;
import com.example.managemyself.data.model.DiaryAttachment;

import java.util.ArrayList;
import java.util.List;

/**
 * 日记附件适配器
 * 用于显示日记的多媒体附件
 */
public class AttachmentAdapter extends RecyclerView.Adapter<AttachmentAdapter.AttachmentViewHolder> {
    
    private List<DiaryAttachment> attachments = new ArrayList<>();
    private OnAttachmentClickListener clickListener;
    private OnAttachmentDeleteListener deleteListener;
    
    public interface OnAttachmentClickListener {
        void onAttachmentClick(DiaryAttachment attachment);
    }
    
    public interface OnAttachmentDeleteListener {
        void onAttachmentDelete(DiaryAttachment attachment);
    }
    
    public AttachmentAdapter(OnAttachmentClickListener clickListener, OnAttachmentDeleteListener deleteListener) {
        this.clickListener = clickListener;
        this.deleteListener = deleteListener;
    }
    
    public void updateAttachments(List<DiaryAttachment> newAttachments) {
        this.attachments.clear();
        if (newAttachments != null) {
            this.attachments.addAll(newAttachments);
        }
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public AttachmentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_diary_attachment, parent, false);
        return new AttachmentViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull AttachmentViewHolder holder, int position) {
        DiaryAttachment attachment = attachments.get(position);
        holder.bind(attachment);
    }
    
    @Override
    public int getItemCount() {
        return attachments.size();
    }
    
    class AttachmentViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivAttachmentIcon;
        private ImageView ivDeleteButton;
        private TextView tvFileName;
        private TextView tvFileSize;
        
        public AttachmentViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAttachmentIcon = itemView.findViewById(R.id.iv_attachment_icon);
            ivDeleteButton = itemView.findViewById(R.id.iv_delete_button);
            tvFileName = itemView.findViewById(R.id.tv_file_name);
            tvFileSize = itemView.findViewById(R.id.tv_file_size);
        }
        
        public void bind(DiaryAttachment attachment) {
            tvFileName.setText(attachment.getFileName());
            tvFileSize.setText(attachment.getFormattedFileSize());
            
            // 设置图标
            int iconRes;
            if (attachment.isPhoto()) {
                iconRes = R.drawable.ic_diary_black_24dp; // 使用现有图标，实际应用中应该有专门的图片图标
            } else if (attachment.isVideo()) {
                iconRes = R.drawable.ic_diary_black_24dp; // 使用现有图标，实际应用中应该有专门的视频图标
            } else if (attachment.isAudio()) {
                iconRes = R.drawable.ic_diary_black_24dp; // 使用现有图标，实际应用中应该有专门的音频图标
            } else {
                iconRes = R.drawable.ic_diary_black_24dp;
            }
            ivAttachmentIcon.setImageResource(iconRes);
            
            // 设置点击事件
            itemView.setOnClickListener(v -> {
                if (clickListener != null) {
                    clickListener.onAttachmentClick(attachment);
                }
            });
            
            ivDeleteButton.setOnClickListener(v -> {
                if (deleteListener != null) {
                    deleteListener.onAttachmentDelete(attachment);
                }
            });
        }
    }
} 